package com.prs.crm.service.stock.impl;

import java.io.File;
import java.io.Serializable;
import java.util.Collection;

import com.prs.crm.context.Attach;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.context.ToolKits;
import com.prs.crm.dao.stock.PurchaseDao;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.stock.Product;
import com.prs.crm.domain.stock.Purchase;
import com.prs.crm.domain.stock.PurchaseDetail;
import com.prs.crm.service.impl.FlowServiceImpl;
import com.prs.crm.service.stock.PurchaseService;

public class PurchaseServiceImpl extends FlowServiceImpl<Purchase, PurchaseDao>
		implements PurchaseService {

	@Override
	public void save(Purchase purchase, Double[] prices, Integer[] stockNumes,
			Double[] discounts, Double[] total, Product[] products,
			String joiners, Integer[] storedNums) {

		for (int i = 0; i < stockNumes.length; i++) {
			PurchaseDetail detail = new PurchaseDetail();
			detail.setProduct(products[i]);
			detail.setStockNum(stockNumes[i]);
			purchase.addDetail(detail);
		}
		Collection<Person> copyPersons = this.getPersonService().findPersons(
				ToolKits.convertStringAryToIntAry(joiners.split(",")));
		purchase.addCopyToPersons(copyPersons);

		super.save(purchase);
	}

	@Override
	public void savePurchase(Purchase purchase, Product[] products, String joiners, Integer[] stockNumes,File file,String fileName) {
		// TODO Auto-generated method stub
		if(file!=null){
			Attach pAttach = FileUtil.getInstance().saveFile(file,
					fileName, "", Constrains.UPLOAD_DIR_PURCHASE);
			purchase.setPurchaseAttach(pAttach.getSrc());}
		for (int i = 0; i < products.length; i++) {
			PurchaseDetail detail = new PurchaseDetail();
			detail.setProduct(products[i]);
			detail.setStockNum(stockNumes[i]);
			purchase.addDetail(detail);
		}
		Collection<Person> copyPersons = this.getPersonService().findPersons(
				ToolKits.convertStringAryToIntAry(joiners.split(",")));
		purchase.addCopyToPersons(copyPersons);

		super.save(purchase);
	}
	
	@Override
	public boolean valideSubmit(Purchase entity) {
		return true;
	}

	public void saveDetail(PurchaseDetail detail) {
		this.getDao().saveDetail(detail);
	}

	public PurchaseDetail getDetail(java.io.Serializable id) {
		return this.getDao().getDetail(id);
	}

	@Override
	public void saveOrUpdate(PurchaseDetail purchaseDetail) {
		this.getDao().saveOrUpdate(purchaseDetail);
	}

	@Override
	public Purchase getPurchase(Serializable id) {
		
		return this.getDao().getPurchase(id);
	}

	@Override
	public void saveOrUpdate(Purchase purchase) {
		this.getDao().saveOrUpdate(purchase);
	}

	
}
